<template>
    <div style="width: 500px">
        <el-input
                size="small"
                placeholder="输入关键字进行过滤"
                prefix-icon="el-icon-search"
                v-model="filterText">
        </el-input>
        <!--:expand-on-click-node="false"点击箭头才会展开-->
        <el-tree
                :default-expand-all="true"
                :expand-on-click-node="false"
                :data="deps"
                :props="defaultProps"
                :filter-node-method="filterNode"
                ref="tree">

            <!--树形控件使用 scoped slot   node当前节点 data绑定的后端完整数据-->
                  <span class="custom-tree-node" slot-scope="{ node, data }" style="display: flex;justify-content: space-between;width:100%">
                        <span>{{ data.name }}</span>
                        <span>
                          <el-button
                                  class="depBtn"
                                  type="primary"
                                  size="mini"
                                  @click="() => showAddDep(data)">
                           添加部门
                          </el-button>
                          <el-button
                                  class="depBtn"
                                  type="danger"
                                  size="mini"
                                  @click="() => deleteDep(data)">
                            删除部门
                          </el-button>
        </span>
      </span>


        </el-tree>

        <el-dialog
                title="添加部门"
                :visible.sync="dialogVisible"
                width="30%"
               >
            <div>

                <table>
                    <tr>
                        <td><el-tag>上级部门</el-tag></td>
                        <td>
                            <el-input
                                v-model="pname"
                                :disabled="true">
                        </el-input></td>
                    </tr>
                    <tr>
                        <td>
                            <el-tag>部门名称</el-tag>
                        </td>
                        <td>
                            <el-input v-model="dep.name" placeholder="请输入部门名称"></el-input>
                        </td>
                    </tr>
                </table>

            </div>
            <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="doAddDep">确 定</el-button>
  </span>
        </el-dialog>


    </div>
</template>

<script>
    export default {
        name: "DepMana",
        data(){
            return{
                dep:{
                    name:'',
                    parentId:-1
                },
                pname:'',
                k:[],

                dialogVisible:false,
                filterText:'',
                deps:[],
                defaultProps: {
                    children: 'children',
                    label: 'name',
                },
            }
        },
        watch: {
            filterText(val) {
                this.$refs.tree.filter(val);
            },
        },


        mounted(){
            this.initDeps();

        },
        methods: {


            initDep() {
                this.dep = {
                    name: '',
                    parentId: -1
                },
                    this.pname = '';
            },
            doAddDep() {
                this.postRequest('/system/basic/department/',this.dep).then(resp => {
                    if (resp) {
                        this.dialogVisible = false;
                      //  this.addDep2Deps(this.deps,resp.obj);
                        this.initDeps();
                        this.initDep();
                    }
                });
            },
          /*  addDep2Deps(deps, dep) {
                for (let i = 0; i < deps.length; i++) {
                    let d =deps[i];
                    if (d.id == dep.parentId) {
                        d.children=d.children.concat(dep);
                     //实时拼接，不需要刷新,又bug，只能连续添加一个子类的子类
                        if (d.children.length>0){
                            d.isParent=true;
                        }
                        return;
                    } else {
                        this.addDep2Deps(d.children, dep);
                    }
                }
            },*/

            filterNode(value, data) {
                if (!value) return true;
                return data.name.indexOf(value) !== -1;
            },
            //初始化获取部门信息
            initDeps() {
                this.getRequest('/system/basic/department/').then(resp => {
                    if (resp) {
                        this.deps = resp;
                    }
                });
            },
            showAddDep(data) {
                this.dialogVisible = true;
                this.pname = data.name;
                this.dep.parentId = data.id;

            },
            deleteDep(data) {
              if (data.isParent){
                  this.$message.error('父部门删除失败');
              }else{
                  this.$confirm('此操作将永久删除 [' + data.name + '] 部门, 是否继续?', '提示', {
                      confirmButtonText: '确定',
                      cancelButtonText: '取消',
                      type: 'warning'
                  }).then(() => {
                      this.deleteRequest('/system/basic/department/' + data.id).then(resp => {
                          if (resp) {
                              //this.removeDepFromDeps(null,this.deps, data.id);
                              this.initDeps();
                          }
                      });
                  }).catch(() => {
                      this.$message({
                          type: 'info',
                          message: '已取消删除'
                      });
                  });
              }
            },
            //删除后手动刷新方法，和addDep2Deps原理差不多
         /*   removeDepFromDeps(p,deps,id){
                for (let i=0;i<deps.length;i++){
                    let d =deps[i];
                    if (d.id===id){
                      deps.splice(i,1);
                        if (deps.length==0){
                            p.isParent=false;
                        }
                        return;
                    }else{
                        this.removeDepFromDeps(d,d.children,id);
                    }
                }
            }
*/











        }


    }
</script>

<style scoped>
.depBtn{
    padding: 2px;
}

</style>
